package com.zyl.simpletextwidget;

import com.zyl.simpletextwidget.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.TextView;

public class SimpleTextWidgetConfigure extends Activity
{
  private static final int DIALOG_BACKOLOR = 2;
  private static final int DIALOG_GRAVITY = 4;
  private static final int DIALOG_TEXTCOLOR = 1;
  private static final int DIALOG_TEXTSIZE = 3;
  private static final String PREFS_NAME = "com.zyl.simpletextwidget.SimpleTextWidgetProvider";
  private static final String PREF_PREFIX_GRAVITY_KEY = "prefix_gravity_";
  private static final String PREF_PREFIX_KEY = "prefix_";
  private static final String PREF_PREFIX_TEXTCOLOR_KEY = "prefix_textcolor_";
  private static final String PREF_PREFIX_TEXTSIZE_KEY = "prefix_textsize_";
  final Context context = this;
  private int gravity;
  private Button gravityButton;
  private TextView gravityTextView;
  private int mAppWidgetId;
  private Button okButton;
  private EditText textEditText;
  private int textcolor;
  private Button textcolorButton;
  private TextView textcolorTextView;
  private int textsize;
  private Button textsizeButton;
	private TextView textsizeTextView;
	private Button   backcolorButton ;
  private TextView   backcolorTextView  ;
  public static void deletePref(Context paramContext, int paramInt)
  {
    SharedPreferences.Editor localEditor = paramContext.getSharedPreferences("com.zyl.simpletextwidget.SimpleTextWidgetProvider", 0).edit();
    localEditor.remove("prefix_" + paramInt);
    localEditor.remove("prefix_textcolor_" + paramInt);
    localEditor.remove("prefix_gravity_" + paramInt);
    localEditor.remove("prefix_textsize_" + paramInt);
    localEditor.commit();
  }

  private String getColor(int who, int color){
	  String mColor=context.getString(R.string.black_color);
	  if(who==DIALOG_BACKOLOR) color=color-1;
	  
	  switch(color){
	   case -1: mColor=context.getString(R.string.no); break; 
	   case 1: mColor=context.getString(R.string.white_color); break; 
	   case 2: mColor=context.getString(R.string.gray_color); break; 
	   case 3: mColor=context.getString(R.string.red_color); break; 
	   case 4: mColor=context.getString(R.string.yellow_color); break; 
	   case 5: mColor=context.getString(R.string.green_color); break; 
	   case 6: mColor=context.getString(R.string.blue_color); break; 
	  }
	  return mColor;
	 }
	 
	 private String getTextSize(int textsize){
	  String mTextSize=context.getString(R.string.size18);
	  
	  switch(textsize){
	 //  case 0: mTextSize=context.getString(R.string.size16); break; 
	   case 1: mTextSize=context.getString(R.string.size18); break; 
	   case 2: mTextSize=context.getString(R.string.size20); break; 
	   case 3: mTextSize=context.getString(R.string.size22); break; 
	   case 4: mTextSize=context.getString(R.string.size24); break; 
	//   case 5: mTextSize=context.getString(R.string.size26); break; 
	  }
	  return mTextSize;
	 }
	 
	 private String getGravity(int gravity){
	  String mGravity=context.getString(R.string.left);
	  switch(gravity){
	   case 1: mGravity=context.getString(R.string.center); break; 
	   case 2: mGravity=context.getString(R.string.right); break; 
	  }
	  return mGravity;
	 }
	 
	   static int setColor(int who, int color){
	  if(who==1) color=color-1;
	  int mColor=0;
	  switch(color){
	      case -1: mColor=0x00000000; break; 
	      case 0: mColor=Color.BLACK; break; 
	   case 1: mColor=Color.WHITE; break; 
	   case 2: mColor=Color.GRAY; break; 
	   case 3: mColor=Color.RED; break; 
	   case 4: mColor=Color.YELLOW; break; 
	   case 5: mColor=Color.GREEN; break; 
	   case 6: mColor=Color.BLUE; break; 
	  }
	  return mColor;
	 }
	  
	   public static int getGravityInt(int gravity){
	  int mGravity=Gravity.LEFT;
	  switch(gravity){
	   case 1: mGravity=Gravity.CENTER; break; 
	   case 2: mGravity=Gravity.RIGHT; break; 
	  }
	  return mGravity;
	 }
	  
	   public static int getTextSizeInt(int textsize){
	  int mTextSize=18;
	  
	  switch(textsize){
	   case 0: mTextSize=16; break; 
	   case 1: mTextSize=18; break; 
	   case 2: mTextSize=20; break; 
	   case 3: mTextSize=22; break; 
	   case 4: mTextSize=24; break; 
	   case 5: mTextSize=26; break; 
	  }
	  return mTextSize;
	 }

  public static int loadGravityPref(Context paramContext, int paramInt)
  {
    return paramContext.getSharedPreferences("com.zyl.simpletextwidget.SimpleTextWidgetProvider", 0).getInt("prefix_gravity_" + paramInt, 0);
  }

  public static int loadTextColorPref(Context paramContext, int paramInt)
  {
    return paramContext.getSharedPreferences("com.zyl.simpletextwidget.SimpleTextWidgetProvider", 0).getInt("prefix_textcolor_" + paramInt, 0);
  }

  public static String loadTextPref(Context paramContext, int paramInt)
  {
    String str1 = paramContext.getSharedPreferences("com.zyl.simpletextwidget.SimpleTextWidgetProvider", 0).getString("prefix_" + paramInt, null);
    if (str1 != null);
    for (String str2 = str1; ; str2 = "")
      return str2;
  }

  public static int loadTextSizePref(Context paramContext, int paramInt)
  {
    return paramContext.getSharedPreferences("com.zyl.simpletextwidget.SimpleTextWidgetProvider", 0).getInt("prefix_textsize_" + paramInt, 0);
  }

  public static void saveTextPref(Context paramContext, int paramInt1, String paramString, int paramInt2, int paramInt3, int paramInt4)
  {
    SharedPreferences.Editor localEditor = paramContext.getSharedPreferences("com.zyl.simpletextwidget.SimpleTextWidgetProvider", 0).edit();
    localEditor.putString("prefix_" + paramInt1, paramString);
    localEditor.putInt("prefix_textcolor_" + paramInt1, paramInt2);
    localEditor.putInt("prefix_gravity_" + paramInt1, paramInt3);
    localEditor.putInt("prefix_textsize_" + paramInt1, paramInt4);
    localEditor.commit();
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.input_text);
//    
//    backcolorButton=(Button)findViewById(R.id.backcolor_Button);
//    backcolorTextView=(TextView)findViewById(R.id.backcolor_TextView);
    
    setContentView(R.layout.input_text);
    
    textEditText=(EditText)findViewById(R.id.text_EditText);
    okButton=(Button)findViewById(R.id.ok_Button);
    textcolorButton=(Button)findViewById(R.id.textcolor_Button);
    textcolorTextView=(TextView)findViewById(R.id.textcolor_TextView);
    textsizeButton=(Button)findViewById(R.id.textsize_Button);
    textsizeTextView=(TextView)findViewById(R.id.textsize_TextView);
    gravityButton=(Button)findViewById(R.id.gravity_Button);
    gravityTextView=(TextView)findViewById(R.id.gravity_TextView);
    Bundle localBundle = getIntent().getExtras();
    if (localBundle != null)
      this.mAppWidgetId = localBundle.getInt("appWidgetId", 0);
    if (this.mAppWidgetId == 0)
      finish();
    this.textcolor = loadTextColorPref(this.context, this.mAppWidgetId);
    this.textsize = loadTextSizePref(this.context, this.mAppWidgetId);
    this.gravity = loadGravityPref(this.context, this.mAppWidgetId);
    this.textEditText.setText(loadTextPref(this, this.mAppWidgetId));
    this.textcolorTextView.setText(getColor(1, this.textcolor));
    this.textsizeTextView.setText(getTextSize(this.textsize));
    this.gravityTextView.setText(getGravity(this.gravity));
    okButton.setOnClickListener(new OnClickListener(){
    	   public void onClick(View arg0) {
    	    String text=textEditText.getText().toString();
    	    saveTextPref(context, mAppWidgetId, text, textcolor,  gravity, textsize);
    	         
    	          AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    	          SimpleTextWidgetProvider.updateTextAppWidget(context, appWidgetManager, mAppWidgetId, text,  textcolor,textsize, gravity);
    	         
    	          Intent resultValue = new Intent();
    	          resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    	          setResult(RESULT_OK, resultValue);
    	          finish();
    	   }
    	        });
    textcolorButton.setOnClickListener(new OnClickListener() {
    	   public void onClick(View v) {
    	    showDialog(DIALOG_TEXTCOLOR);
    	   }
    	  });
    	       
//    	        backcolorButton.setOnClickListener(new OnClickListener() {
//    	   public void onClick(View v) {
//    	    showDialog(DIALOG_BACKOLOR);
//    	   }
//    	  });
    	       
    	        textsizeButton.setOnClickListener(new OnClickListener() {
    	   public void onClick(View v) {
    	    showDialog(DIALOG_TEXTSIZE);
    	   }
    	  });
    	       
    	        gravityButton.setOnClickListener(new OnClickListener() {
    	   public void onClick(View v) {
    	    showDialog(DIALOG_GRAVITY);
    	   }
    	  });
  }

  @Override
  protected Dialog onCreateDialog(int resId) {
   if(resId==DIALOG_TEXTCOLOR){
    return new AlertDialog.Builder(this)
          .setTitle(R.string.select_button)
          .setSingleChoiceItems(R.array.textcolors, textcolor, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                   textcolor=whichButton;
                  }
              })
          .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
               textcolorTextView.setText(getColor(DIALOG_TEXTCOLOR,textcolor));
              }
          })
         .create();
//   }else if(resId==DIALOG_BACKOLOR){
//    return new AlertDialog.Builder(this)
//          .setTitle(R.string.select_button)
//          .setSingleChoiceItems(R.array.backcolors, backcolor, new DialogInterface.OnClickListener() {
//                  public void onClick(DialogInterface dialog, int whichButton) {
//                   backcolor=whichButton;
//                  }
//              })
//          .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
//              public void onClick(DialogInterface dialog, int whichButton) {
//               backcolorTextView.setText(getColor(DIALOG_BACKOLOR,backcolor));
//              }
//          })
//         .create();
//    
   }else if(resId==DIALOG_TEXTSIZE){
    return new AlertDialog.Builder(this)
          .setTitle(R.string.select_button)
          .setSingleChoiceItems(R.array.textsize, textsize, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                   textsize=whichButton;
                  }
              })
          .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
               textsizeTextView.setText(getTextSize(textsize));
              }
          })
         .create();
   }else if(resId==DIALOG_GRAVITY){
    return new AlertDialog.Builder(this)
          .setTitle(R.string.select_button)
          .setSingleChoiceItems(R.array.gravity, gravity, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                   gravity=whichButton;
                  }
              })
          .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
               gravityTextView.setText(getGravity(gravity));
              }
          })
         .create();
   }
   
   return null;
  }
}
